<!DOCTYPE html>
<html lang="ch" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="../../../static/css/inputLine.css" rel="stylesheet">
    <link href="../../../static/css/create.css" rel="stylesheet">
</head>
<body>
<div class="main-container">
    <h1>创建活动</h1>
    <hr>
    <form id="form" class="input-container">
        <label>
            <span>活动名: </span>
            <input id="name" type="text" placeholder="请输入活动名">
        </label>
        <label>
            <span>活动描述</span>
            <input id="description" type="tel" placeholder="请描述该活动">
        </label>
        <label>
            <button type="submit" id="submit">创建活动</button>
        </label>
    </form>
</div>
</body>
</html>
<script th:src="@{/static/js/anime.min.js}"></script>
<script th:src="@{/static/js/index.js}"></script>
<script th:src="@{/static/lib/jquery/jquery-3.7.1.min.js}"></script>
<script th:src="@{/static/lib/layer/layer.js}"></script>
<script>
    $("#submit").click(
        function (event) {
            event.preventDefault();
            const name = $("#name").val();
            const description = $("#description").val();
            if (name === "") {
                layer.msg("活动名不能为空", {icon: 2, time: 1000});
                return;
            }
            if (description === ""){
                layer.msg("活动描述不能为空", {icon: 2, time: 1000});
                return;
            }
            const submitData = {
                "name":name,
                "desc":description
            };
            // 通过ajax向后台发送请求及响应
            console.log(submitData);
            $('#form')[0].reset();
            $.ajax({
                url: "[[@{/}]]doCreate",
                type: "post",
                // 对表单数据对象序列化
                data: JSON.stringify(submitData),
                // 发送数据的格式和编码
                contentType: "application/json; charset=utf-8",
                // 返回值的类型
                dataType: "json",
                // 回调函数
                success: function (data) {
                    console.log(data.code + ", " + data.msg);
                    if (data.code === 200) {
                        // 登录成功，提示
                        layer.msg(data.msg, {icon: 1, time: 800})
                    } else {
                        layer.msg(data.msg, {icon: 2, time: 2000})
                    }
                },
                error: function (data) {
                    layer.msg(data.msg, {icon: 1, time: 800});
                }
            })

        }
    );
</script>